<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Statement
 *
 * @author zelic
 */
class Statement {

    var $statement = "";

    public function selectStatement($table, $conditions = null, $fields = null) {
        $f = "*";
        $w = "";
        if (is_array($fields)) {
            $f = implode(",", $fields);
        } else if (is_string($fields)) {
            $f = $fields;
        }
        if (is_array($conditions)) {
            foreach ($conditions as $key => $value) {
                if (is_string($value)) {
                    $conditionArray[] = "$key='$value'";
                } else {
                    $conditionArray[] = "$key=$value";
                }
            }
            $w = implode(",", $conditionArray);
        } else if (is_string($conditions)) {
            $w = $conditions;
        }

        if ($w != "") {
            $this->statement = "SELECT $f FROM $table WHERE $w";
        } else {
            $this->statement = "SELECT $f FROM $table";
        }
    }

    public function insertStatement($table, $data = null) {
        if (is_array($data)) {
            $fields = array_keys($data);
            $values = array_values($data);
            for ($i = 0; $i < count($values); $i++) {
                if (is_string($values[$i])) {
                    $values[$i] = "'$values[$i]'";
                }
            }
            $fieldString = implode(",", $fields);
            $valueString = implode(",", $values);
            $this->statement = "INSERT INTO $table($fieldString) VALUES($valueString)";
        }
    }

    public function updateStatement($table, $conditions, $data) {
        if (is_array($data)) {
            $setArray = array();
            $conditionArray = array();
            $conditionString = "";

            foreach ($data as $key => $value) {
                if (is_string($value)) {
                    $setArray[] = "$key='$value'";
                } else {
                    $setArray[] = "$key=$value";
                }
            }
            $setString = implode(",", $setArray);

            if (is_array($conditions)) {
                foreach ($conditions as $key => $value) {
                    if (is_string($value)) {
                        $conditionArray[] = "$key='$value'";
                    } else {
                        $conditionArray[] = "$key=$value";
                    }
                }
                $conditionString = implode(",", $conditionArray);
            } else if (is_string($conditions)) {
                $conditionString = $conditions;
            }
            $this->statement = "UPDATE $table SET $setString WHERE $conditionString ";
        }
    }

    public function deleteStatement($table, $conditions) {
        $conditionArray = array();
        $conditionString = "";
        if (is_array($conditions)) {
            foreach ($conditions as $key => $value) {
                if (is_string($value)) {
                    $conditionArray[] = "$key='$value'";
                } else {
                    $conditionArray[] = "$key=$value";
                }
            }
            $conditionString = implode(",", $conditionArray);
        } else if (is_string($conditions)) {
            $conditionString = $conditions;
        }
        $this->statement = "DELETE FROM $table WHERE $conditionString";
    }

    public function __toString() {
        return $this->statement;
    }

}

?>
